<template>
  <div>
    <el-dialog :show-close="false" center width="30%" :visible.sync="dialogFormVisible">
      <slide-verify
        ref="slideblock"
        @again="onAgain"
        @fulfilled="onFulfilled"
        @success="onSuccess"
        @fail="onFail"
        @refresh="onRefresh"
        :accuracy="accuracy"
        :slider-text="text"
      ></slide-verify>
    </el-dialog>

    <div class="whole_black">
      <el-card class="box-card">
        <h3 slot="header" class="login-total">手机号登录</h3>
        <div>
          <el-form
            v-loading="loading"
            :rules="userFormRules"
            ref="ruleForm"
            label-position="left"
            label-width="20px"
            :model="userForm"
          >
            <el-form-item prop="phone">
              <el-input
                prefix-icon="el-icon-mobile-phone"
                v-model="userForm.phone"
                placeholder="请输入手机号"
                clearable
              ></el-input>
            </el-form-item>
            <div></div>
            <el-form-item class="code-input" prop="code" show-password>
              <el-row>
                <el-col :span="18">
                  <el-input
                    prefix-icon="el-icon-chat-dot-round"
                    v-model="userForm.code"
                    placeholder="请输入验证码"
                    clearable
                    @keyup.native.enter="submitForm('ruleForm')"
                  ></el-input>
                </el-col>
                <el-col :span="4" :offset="2">
                  <el-tooltip class="item" effect="dark" content="点击发送验证码" placement="top">
                    <el-button @click="vertifySlide" icon="el-icon-thumb" type="primary" circle></el-button>
                  </el-tooltip>
                </el-col>
              </el-row>
            </el-form-item>
            <el-form-item class="operation">
              <el-button type="primary" @click="submitForm('ruleForm')">确认</el-button>
              <el-button type="warning" @click="backLogin">返回</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  name: "LoginPhone",
  data() {
    return {
      userForm: {
        phone: "16776149495",
        code: ""
      },
      userFormRules: {
        phone: [
          { required: true, message: "请输入手机号", trigger: "change" },
          {
            pattern: /^1[3456789]\d{9}$/,
            message: "请输入正确的号码格式",
            trigger: "change"
          }
        ],
        code: [{ required: true, message: "验证码不能为空", trigger: "change" }]
      },
      dialogFormVisible: false,
      loading: false,
      msg: "",
      text: "向右滑",
      // 精确度小，可允许的误差范围小；为1时，则表示滑块要与凹槽完全重叠，才能验证成功。默认值为5
      accuracy: 5
    };
  },
  methods: {
    backLogin() {
      this.$router.push("/login");
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.loading = true;
          this.userLogin();
          // 3秒没有相应则关闭动画
        } else {
          this.$message.warning("信息不完整！！");
        }
      });
    },
    userLogin() {
      let params = new URLSearchParams();
      params.append("cardNum", this.userForm.cardNum);
      params.append("password", this.userForm.password);
      // 开启动画
      setTimeout(() => {
        this.loading = false;
      }, 3000);
      this.$http
        .post(
          `user/noAuthorize/loginUser/${this.userForm.phone}/${this.userForm.code}`
        )
        .then(resp => {
          // 关闭动画
          this.loading = false;
          let { code, msg, data } = resp.data;
          let { token } = data;
          if (code === 1) {
            sessionStorage.setItem("USER-TOKEN", token);
            this.$router.push("/welcome");
            return this.$message.success("登录成功,欢迎来到阿布校理！");
          } else {
            this.$message.error(msg);
          }
          this.loading = false;
        })
        .catch(resp => {
          this.loading = false;
        });
    },
    vertifySlide() {
      this.onRefresh();
      this.$refs.ruleForm.validateField("phone", valid => {
        if (!valid) {
          //  发送验证码
          this.dialogFormVisible = true;
        }
      });
    },
    sendCodeToPhone() {
      this.$http
        .get(`user/noAuthorize/phone/${this.userForm.phone}`)
        .then(resp => {
          let { code, msg } = resp.data;
          if (code == 1) {
            return this.$message.success(msg);
          }
          return this.$message.warning(msg);
        })
        .catch(resp => {});
    },
    //滑块验证

    // 验证成功，触发该方法
    onSuccess(times) {
      let ms = (times / 1000).toFixed(1);
      console.log("耗时" + ms + "毫秒");
      // 关闭滑动窗口
      this.dialogFormVisible = false;
      // 发送验证
      this.sendCodeToPhone();
    },
    onFail() {
      this.$message.warning("验证失败，请重新验证");
    },
    onRefresh() {
      console.log("刷新成功");
    },
    onFulfilled() {
      this.onRefresh();
      console.log("刷新成功啦！");
    },
    onAgain() {
      this.$message.error("检测到非人为操作的哦！");
      this.msg = "try again";
      // 刷新
      this.$refs.slideblock.reset();
    },
    handleClick() {
      // 父组件直接可以调用刷新方法
      this.$refs.slideblock.reset();
    }
  }
};
</script >
 
<style scoped >
.whole_black {
  top: 0;
  bottom: 0;
  position: fixed;
  overflow-y: scroll;
  overflow-x: hidden;
  width: 100%;
  height: 100%;
  background-image: url(../../assets/bg.jpg);
  background-repeat: no-repeat;
  background-size: 100% 100%;
}
.box-card {
  border-radius: 15px;
  width: 340px;
  height: 360px;
  margin-top: 130px;
  margin-left: 780px;
  box-shadow: 5px 5px 5px;
}

.login-total {
  text-align: center;
}

.imgLink {
  width: 30px;
  height: 30px;
}

.el-form-item {
  margin-right: 20px;
}
.operation {
  margin-left: 60px;
}
.slide-verify {
  margin-left: 20px;
}

.el-dialog__body {
  width: 100px;
}
</style>